//
//  SwiftExtension.swift
//  SwiftYC
//
//  Created by tenltrs on 2018/9/21.
//  Copyright © 2018年 Tenltrs. All rights reserved.
//

import Foundation
import Kingfisher

private var SessionManagerKey: String = ""

private var SelectedIndexKey : String = ""

extension UIColor{
    
    ///导航黄
    static let NavColor =  UIColor(patternImage: UIImage(named: "ic_me_banner_backgroud_375x180_")!.byCrop(to: CGRect(x: 0, y: 0, width: 5, height: 5))!)
    
    static let BackgroundColor = UIColor(hexString: "#f5f5f5")!
    
    static let BLACK3TEXTCOLOR = UIColor(hexString: "#333333")!
    
    static let BLACK5TEXTCOLOR = UIColor(hexString: "#555555")!
    
    static let BLACK6TEXTCOLOR = UIColor(hexString: "#666666")!
    
    static let BLACK9TEXTCOLOR = UIColor(hexString: "#999999")!
    
    static let LineColor = UIColor(hexString: "#e6e6e6")!
    
    /// 着重色
    static let FocusColor = UIColor(hexString: "#43250C")!
}


extension UIViewController{
    
    ///当前控制器 请求回话
    var sessionManager : SessionManager?{
        get {
            return (objc_getAssociatedObject(self, &SessionManagerKey) as? SessionManager)
        }
        set(newValue) {
            objc_setAssociatedObject(self, &SessionManagerKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
        }
    }

    
    func ts_addChild(_ childVC: UIViewController, toView: UIView) {
        childVC.beginAppearanceTransition(true, animated: false)
        toView.addSubview(childVC.view)
        childVC.endAppearanceTransition()
        childVC.didMove(toParent: self)
    }
    
}

extension UIImageView{
    
    func ts_loadImage(url : String?) {
        
        ts_loadImage(url: url, placeholderImage: placeholderImageStr);
    }
    
    func ts_loadImage(url : String?,placeholderImage : String?) {

        ts_loadImage(url: url, placeholderImage: placeholderImage, completionHandler: nil)
    }
    
    func ts_loadImage(url : String?, completionHandler : SDExternalCompletionBlock?){
        
        ts_loadImage(url: url, placeholderImage: placeholderImageStr, completionHandler: completionHandler);
    }
    
    func ts_loadImage(url : String?, placeholderImage : String?, completionHandler : SDExternalCompletionBlock?) {

        weak var ws = self
        self.sd_setImage(with: URL(string: url ?? ""), placeholderImage: (placeholderImage != nil) ? UIImage(named:placeholderImage!) : nil, options: .refreshCached, progress: nil) { (image, error, type, url) in
            
            if (image != nil){
                
                let anim = CATransition()
                anim.duration=0.5
                anim.type = CATransitionType.fade
                anim.isRemovedOnCompletion=true
                ws?.layer.add(anim, forKey: "transition")
            }
            if (completionHandler != nil){
                completionHandler!(image,error,type,url)
            }
            
        }
        self.layer.removeAnimation(forKey: "transition")
    }
}


extension Array {
    subscript (safe index: Int) -> Element? {
        return (0..<count).contains(index) ? self[index] : nil
    }
}
